<?php
namespace App\Service\Admin\Qudao;

use App\Service\Admin\BaseService as Service;

class CostService extends Service
{
    //费用

    protected $types = [
        1 => '会员充值', '保证金',
    ];

    /**
     * 列表
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->db2Conn($this->dbEqCost)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $tname = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'tname' => $tname,
                'cost' => $v['cost'] / 100,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function add($pas)
    {
        $pas['addtime'] = toDate();
        $up = $this->db2Conn($this->dbEqCost)->insertGetId($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id 分类ID
     */
    public function setDel($id)
    {
        $res = (array) $this->db2Conn($this->dbEqCost)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '分类不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->db2Conn($this->dbEqCost)->where('id', $id)->update($arys);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }
}